package github.remonlong.restfulutil.exception;

import com.sun.istack.internal.Nullable;
import github.remonlong.restfulutil.exception.api.BadApiException;
import github.remonlong.restfulutil.exception.enumeration.EnumResult;
import github.remonlong.restfulutil.exception.util.StringUtils;


/**
 * 数据校验类
 * @author remon
 * @create 2018-09-21 12:13
 **/
public class RemonAssert {


    public RemonAssert() {
    }

    public static void state(boolean expression, String message) {
        if (!expression) {
            throw new IllegalStateException(message);
        }
    }


    /**
     * @deprecated
     */
    @Deprecated
    public static void state(boolean expression) {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

    public static void isTrue(boolean expression, EnumResult result) {
        if (!expression) {
            throw new BadApiException(nullSafeGet(result));
        }
    }

    public static void isNull( Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isNull( Object object, EnumResult result) {
        if (object != null) {
            throw new BadApiException(nullSafeGet(result));
        }
    }


    public static void notNull( Object object, EnumResult result) {
        if (object == null) {
            throw new BadApiException(nullSafeGet(result));
        }
    }

    public static void hasLength( String text, EnumResult result) {
        if (!StringUtils.hasLength(text)) {
            throw new BadApiException(result);
        }
    }


    public static void hasText( String text, EnumResult result) {
        if (!StringUtils.hasText(text)) {
            throw new BadApiException(result);
        }
    }


    public static void doesNotContain( String textToSearch, String substring, EnumResult result) {
        if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) {
            throw new BadApiException(result);
        }
    }


    public static void notEmpty( Object[] array, EnumResult enumResult) {
        if (objectEmpty(array)) {
            throw new BadApiException(enumResult);
        }
    }

    private static boolean objectEmpty( Object[] array) {
        return array == null || array.length == 0;
    }


    public static void noNullElements( Object[] array, EnumResult enumResult) {
        if (array != null) {
            Object[] var2 = array;
            int var3 = array.length;
            for (int var4 = 0; var4 < var3; ++var4) {
                Object element = var2[var4];
                if (element == null) {
                    throw new BadApiException(enumResult);
                }
            }
        }

    }


    public static void isInstanceOf(Class<?> type,  Object obj, EnumResult result) {
        notNull(type, result);
        if (!type.isInstance(obj)) {
            instanceCheckFailed(type, obj, result);
        }

    }

    public static void isInstanceOf(Class<?> type,  Object obj) {
        isInstanceOf(type, obj, EnumResult.CLASS_INSTANT_FAIL);
    }


    private static void instanceCheckFailed(Class<?> type,  Object obj,  EnumResult msg) {
        String className = obj != null ? obj.getClass().getName() : "null";
        String result = "";
        boolean defaultMessage = true;

        throw new BadApiException(msg);
    }

    private static void assignableCheckFailed(Class<?> superType,  Class<?> subType,  EnumResult result) {

        throw new BadApiException(result);
    }

    private static boolean endsWithSeparator(String msg) {
        return msg.endsWith(":") || msg.endsWith(";") || msg.endsWith(",") || msg.endsWith(".");
    }

    private static String messageWithTypeName(String msg,  Object typeName) {
        return msg + (msg.endsWith(" ") ? "" : ": ") + typeName;
    }

    private static EnumResult nullSafeGet( EnumResult enumResult) {
        return enumResult != null ? enumResult : null;
    }

}
